import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'Base/InitConfig.dart';
import 'Base/Util/ShakeManager.dart';
import 'Base/methodChannel/MethodChannelManager.dart';
import 'Base/redux/reduxConfig.dart';
import 'Base/router/RouterConfig.dart';
import 'Base/style/MyStyles.dart';

class Application extends StatefulWidget {
  @override
  _ApplicationState createState() => _ApplicationState();
}

class _ApplicationState extends State<Application> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (InitConfig.ENV == PUBLISHING_ENVIRONMENT.DEV) {
        ShakeManager.getInstance().startListening();
        Fluttertoast.showToast(msg: "当前为测试环境", toastLength: Toast.LENGTH_LONG);
      } else if (InitConfig.ENV == PUBLISHING_ENVIRONMENT.PROD) {
        Future.delayed(Duration(seconds: 1)).then((value) {
          MethodChannelManager.getInstance().invoke(MethodTypes.CHECK_UPDATE);
        });
      }
    });
  }

  @override
  void dispose() {
    if (InitConfig.ENV == PUBLISHING_ENVIRONMENT.DEV) {
      ShakeManager.getInstance().stopListening();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return StoreProvider<AppState>(
      store: appStore,
      child: MaterialApp(
        title: "纸嘟嘟消息助手",
        initialRoute: "/",
        routes: RouterMap,
        navigatorObservers: [MyNavigatorObserver.getInstance()],
        onUnknownRoute: (settings) {
          return PageRouteBuilder(pageBuilder: (context, __, ___) {
            return PageErr();
          });
        },
        theme: ThemeData(
//                  brightness: Brightness.dark,
            primaryColor: MyColors.PRIMARY_COLOR,
            accentColor: MyColors.SECOND_COLOR,
            appBarTheme: AppBarTheme(
              centerTitle: true,
              iconTheme: IconThemeData(color: Colors.white),
            )),
        builder: (BuildContext context, Widget child) {
          // 确保 loading 组件能覆盖在其他组件之上.
          return FlutterEasyLoading(child: child);
        },
      ),
    );
  }
}